package com.aurora.source.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.Toolbar;

import com.aurora.source.R;

/**
 * @author: Lance
 * @description: 自定义 Toolbar
 * @date: 2018/7/10 16:44
 */
@SuppressLint("NewApi")
public class SimpleToolbar extends Toolbar {

    /**
     * 左侧Title
     */
    private TextView mTvLeftTitle;
    /**
     * 中间Title
     */
    private TextView mTvMiddleTitle;
    /**
     * 右侧Title
     */
    private TextView mTvRightTitle;

    public SimpleToolbar(Context context) {
        super(context, null);
    }

    public SimpleToolbar(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public SimpleToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTvLeftTitle = (TextView) findViewById(R.id.tv_left_title);
        mTvMiddleTitle = (TextView) findViewById(R.id.tv_main_title);
        mTvRightTitle = (TextView) findViewById(R.id.tv_right_title);
    }

    // 设置中间title的内容
    public void setMainTitle(String text) {
        this.setTitle("");
        mTvMiddleTitle.setVisibility(View.VISIBLE);
        mTvMiddleTitle.setText(text);
    }

    // 设置中间title的内容文字的颜色
    public void setMainTitleColor(int color) {
        mTvMiddleTitle.setTextColor(color);
    }

    // 设置title左边文字
    public void setLeftTitleText(String text) {
        mTvLeftTitle.setVisibility(View.VISIBLE);
        mTvLeftTitle.setText(text);
    }

    // 设置title左边文字颜色
    public void setLeftTitleColor(int color) {
        mTvLeftTitle.setTextColor(color);
    }

    // 设置title左边图标
    public void setLeftTitleDrawable(int res) {
        mTvLeftTitle.setVisibility(View.VISIBLE);
        Drawable dwLeft = ContextCompat.getDrawable(getContext(), res);
        dwLeft.setBounds(0, 0, dwLeft.getMinimumWidth(), dwLeft.getMinimumHeight());
        mTvLeftTitle.setCompoundDrawables(dwLeft, null, null, null);
    }

    // 设置title左边点击事件
    public void setLeftTitleClickListener(OnClickListener onClickListener) {
        mTvLeftTitle.setOnClickListener(onClickListener);
    }

    // 设置title左边 点击 后退
    public void finishLeft(Activity activity) {
        activity.finish();
    }

    // 设置title左边 展示状态
    public void setLeftVisibility(int visibility) {
        mTvLeftTitle.setVisibility(visibility);
    }

    // 设置title右边文字
    public void setRightTitleText(String text) {
        mTvRightTitle.setVisibility(View.VISIBLE);
        mTvRightTitle.setText(text);
    }

    // 设置title右边文字颜色
    public void setRightTitleColor(int color) {
        mTvRightTitle.setTextColor(color);
    }

    // 设置title右边图标
    public void setRightTitleDrawable(int res) {
        mTvRightTitle.setVisibility(View.VISIBLE);
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        mTvRightTitle.setCompoundDrawables(null, null, dwRight, null);
    }

    // 清空title右边图标
    public void setRightTitleDrawable() {
        mTvRightTitle.setCompoundDrawables(null, null, null, null);
    }

    // 设置title右边 展示状态
    public void setRightVisibility(int visibility) {
        mTvRightTitle.setVisibility(visibility);
    }

    // 设置title右边点击事件
    public void setRightTitleClickListener(OnClickListener onClickListener) {
        mTvRightTitle.setOnClickListener(onClickListener);
    }


}
